﻿using System;
using System.Text.RegularExpressions;
using System.Windows;
using System.Windows.Controls;

namespace Twilight.Controls
{
    public class TweetInfo : TextWrapPanel
    {
        private string timeAgo;
        private string from; 

        public TweetInfo()
        {
            linkRegex = new Regex("\\<a(\\srel=\"\\w+\")?(\\starget=\"\\w+\")?\\shref=('|\")(?<link>(.|\n)*?)('|\")>(?<text>(.|\n)*?)<\\/a>", RegexOptions.Singleline | RegexOptions.ExplicitCapture);
        }

        public string PostDate
        {
            get { return (string)GetValue(PostDateProperty); }
            set { SetValue(PostDateProperty, value); }
        }

        // Using a DependencyProperty as the backing store for PostDate.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty PostDateProperty =
            DependencyProperty.Register("PostDate", typeof(string), typeof(TweetInfo), new PropertyMetadata(PostDateChanged));

        public static void PostDateChanged(DependencyObject o, DependencyPropertyChangedEventArgs e)
        {
            ((TweetInfo)o).OnPostDateChanged((string)e.NewValue);
        }

        private void OnPostDateChanged(string postDate)
        {
            timeAgo = ParseDateAndGetTimeAgoString(postDate);
            OnPostDateOrFromChanged();
        }

        public string From
        {
            get { return (string)GetValue(FromProperty); }
            set { SetValue(FromProperty, value); }
        }

        // Using a DependencyProperty as the backing store for From.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty FromProperty =
            DependencyProperty.Register("From", typeof(string), typeof(TweetInfo), new PropertyMetadata(FromChanged));

        public static void FromChanged(DependencyObject o, DependencyPropertyChangedEventArgs e)
        {
            ((TweetInfo)o).OnFromChanged((string)e.NewValue);
        }

        private void OnFromChanged(string from)
        {
            this.from = from;
            OnPostDateOrFromChanged();
        }

        private void OnPostDateOrFromChanged()
        {
            this.Children.Clear();

            if (!string.IsNullOrEmpty(timeAgo) && !string.IsNullOrEmpty(from))
            {
                OnTextChanged(string.Format("{0} from {1}", timeAgo, from));
            }
        }

        protected override void ProcessLinkMatch(Match match)
        {
            Children.Add(new HyperlinkButton()
            {
                Content = match.Groups["text"].Value,
                NavigateUri = new Uri(match.Groups["link"].Value),
                Style = LinkStyle
            });
        }

        private string ParseDateAndGetTimeAgoString(string created_at)
        {
            var created = DateTime.ParseExact(created_at.Replace("+0000", "+00:00"), "ddd MMM dd HH:mm:ss zzz yyyy", null); // date format looks like 'Sat Jan 24 01:43:34 +0000 2009'
            double totalMinutes = DateTime.Now.Subtract(created).TotalMinutes;

            if (totalMinutes < 1)
            {
                return "less than a minute ago";
            }
            else if (totalMinutes < 2)
            {
                return "1 minute ago";
            }
            else if (totalMinutes < 60)
            {
                return string.Format("{0} minutes ago", Math.Round(DateTime.Now.Subtract(created).TotalMinutes), 0);
            }
            else if (totalMinutes < 120)
            {
                return "1 hour ago";
            }
            else if (totalMinutes < 1440)
            {
                return string.Format("{0} hours ago", Math.Round(DateTime.Now.Subtract(created).TotalHours), 0);
            }
            else if (totalMinutes >= 1440 && totalMinutes < 2880)
            {
                return "1 day ago";
            }
            else
            {
                return string.Format("{0} days ago", Math.Round(DateTime.Now.Subtract(created).TotalDays), 0);
            }
        }
    }
}
